<template>
    <div class="apiSearchContainer">
        <span class="inputLabel">微服务名称</span>
        <el-select v-model="configCenterSearchData.application" placeholder="微服务名称" class="searchInfoInput" clearable>
            <el-option :label="item.applicationName" :value="item.applicationName" v-for="item in applicationList" :key="item.id"/>
        </el-select>

        <span class="inputLabel">微服务环境</span>
        <el-input v-model="configCenterSearchData.profile" placeholder="微服务名称" class="searchInfoInput"></el-input>

        <span class="inputLabel">键</span>
        <el-input v-model="configCenterSearchData.key" placeholder="键" class="searchInfoInput"></el-input>

        <span class="inputLabel">值</span>
        <el-input v-model="configCenterSearchData.value" placeholder="值" class="searchInfoInput"></el-input>

        <el-button type="primary" icon="SearchOutlined" :loading="searchLoading" class="searchButtonClass" @click="queryApiInfo()">搜索</el-button>
        <el-button icon="RedoOutlined" @click="resetInput()">重置</el-button>
    </div>
</template>

<script name="ServiceConfigSearch" setup lang="ts">
import {applicationInfoQuery} from "@/request/service";
import {configInfoQuery} from "@/request/configCenter";
import {computed, ref} from "vue";
import {useServiceConfigStore} from "@/stores/serviceManage/serciceConfig";

let applicationList = ref([]);
let searchLoading = ref(false);

let configCenterSearchData = computed(() => {return useServiceConfigStore().configCenterSearchData});

// 查询应用列表
applicationInfoQuery(configCenterSearchData.value).then(res => {
    let global = {
        id: 0,
        applicationName: "GLOBAL"
    }
    res.data.unshift(global)
    applicationList.value = res.data
})

function queryApiInfo() {
    searchLoading.value = true
    configInfoQuery(configCenterSearchData.value).then(res => {
        useServiceConfigStore().resetConfigCenterDataList(res.data);
        searchLoading.value = false
    }).catch(res => {
        searchLoading.value = false
    })
}

function resetInput() {
    let searchData = {
        application: '',
        profile: '',
        key: '',
        value: '',
        pageInfo: {
            pageIndex: configCenterSearchData.value["pageInfo"] === undefined ? 1 : configCenterSearchData.value["pageInfo"].pageIndex,
            pageSize: configCenterSearchData.value["pageInfo"] === undefined ? 10 : configCenterSearchData.value["pageInfo"].pageSize
        }
    }
    useServiceConfigStore().resetConfigCenterSearchData(searchData);
}
</script>

<style scoped lang="less">
.apiSearchContainer {
    width: 100%;
    height: 10%;
    .searchInfoInput {
        width: 15%;
        padding-left: 1%;
    }
    .inputLabel {
        padding-left: 1%;
        font-weight: 700;
        font-size: 15px;
        color: #606266;
    }
    .searchButtonClass {
        margin-left: 1%;
    }
}
</style>